package com.spider.site.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.stream.Collectors;

import org.assertj.core.util.Arrays;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.spider.site.dao.SpiderHistoryResultDao;
import com.spider.site.dao.SpiderResultDao;
import com.spider.site.dao.UserDao;
import com.spider.site.entity.SpiderHistoryResult;
import com.spider.site.entity.SpiderResult;
import com.spider.site.entity.User;
import com.spider.site.service.SpiderService;

/**
 * 蜘蛛侠 服务区
 * 
 * @author wq
 *
 */
@Service
public class SpiderServiceImpl implements SpiderService {

	@Autowired
	private SpiderResultDao spiderResultDao;// 蜘蛛侠 爬取结果 数据层

	@Autowired
	private SpiderHistoryResultDao spiderHistoryResultDao;// 蜘蛛侠 历史爬取结果 数据层

	@Autowired
	private UserDao userDao;// 用户 数据层

	@Override
	public List<String> getAllUrl() {
		return this.spiderResultDao.getAll().stream().map(SpiderResult::getUrl).collect(Collectors.toList());
	}

	@Override
	public boolean addUrl(SpiderResult spider) {
		return this.spiderResultDao.saveSpiderResult(spider);
	}

	@Override
	public boolean checkURL(String url) {
		SpiderResult spiderResult = this.spiderResultDao.getSpiderResultByURL(url);
		if (spiderResult == null) {
			return true;
		}
		return false;
	}

	@Override
	public SpiderResult getByURL(String url) {
		return this.spiderResultDao.getSpiderResultByURL(url);
	}

	@Override
	public List<SpiderResult> getAll() {
		return this.spiderResultDao.getAll();
	}

	@Override
	public boolean editUrl(SpiderResult spider) {
		return this.spiderResultDao.editSpiderResult(spider);
	}

	@Override
	public boolean removeUrl(long[] ids) {
		return Arrays.asList(ids).stream().allMatch(id -> this.spiderResultDao.removeSpiderResult((long) id));
	}

	@Override
	public SpiderResult getById(long id) {
		return this.spiderResultDao.getSpiderResultById(id);
	}

	@Override
	public boolean spinResult(int spin, long id) {
		SpiderResult spiderResult = this.spiderResultDao.getSpiderResultById(id);
		if (spiderResult != null && spiderResult.getIsSpin() != spin) {
			int result = this.spiderResultDao.editSpiderSpin(spin, id);
			if (result > 0) {
				return true;
			}
		}
		return false;
	}

	@Override
	public List<SpiderHistoryResult> getSpiderHistoryResultBySid(long sid) {
		return spiderHistoryResultDao.getSpiderHistoryResultBySid(sid);
	}

	@Override
	public boolean addSpiderHistoryResult(SpiderHistoryResult spiderHistoryResult) {
		return spiderHistoryResultDao.addSpiderHistoryResult(spiderHistoryResult);
	}

	@Override
	public boolean editSpiderHistoryResult(SpiderHistoryResult spiderHistoryResult) {
		return spiderHistoryResultDao.editSpiderHistoryResult(spiderHistoryResult);
	}

	@Override
	public JSONArray getDateBySpiderResultId(long id) {
		List<SpiderHistoryResult> spiderHistoryResultBySid = this.spiderHistoryResultDao
				.getSpiderHistoryResultBySid(id);
		JSONArray json = new JSONArray();
		SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
		spiderHistoryResultBySid.forEach(history -> {
			json.add(f.format(history.getSpinDate()));
		});
		return json;
	}

	@Override
	public JSONArray getHistoryResult(long id, String history) {
		try {
			SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
			SpiderHistoryResult historyResult = this.spiderHistoryResultDao.getSpiderHistoryResultByIdAndDate(id,
					f.parse(history));
			return JSONArray.parseArray(historyResult.getComments());
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public boolean isLogin(User user) {
		User u = this.userDao.getByLoginId(user.getLoginId());// 获取用户信息
		if (u != null) {
			if (u.getPassword().equals(user.getPassword())) {
				return true;
			}
		}
		return false;
	}

}
